//
//  CZEmoticonCell.swift
//  表情键盘
//
//  Created by Jack.Lin on 16/7/19.
//  Copyright © 2016年 Jack.Lin. All rights reserved.
//

import UIKit

// 表情 cell 的协议
@objc protocol CZEmoticonCellDelegate: NSObjectProtocol {
    
    /// 表情 cell 选中表情模型
    /// em: 表情模型 / nil 表示删除
    func emoticonCellDidSelectedEmoticon(cell: CZEmoticonCell, em: JKEmoticon?)
}

class CZEmoticonCell: UICollectionViewCell {
    
    weak var delegate: CZEmoticonCellDelegate?
    
    var emoticons: [JKEmoticon]? {
       
        didSet {
            // 1. 隐藏所有的按钮
            for v in contentView.subviews {
                v.isHidden = true
            }
            
            // 显示删除按钮
            contentView.subviews.last?.isHidden = false
            
            // 2. 遍历表情模型数组，设置按钮图像
            for (i, em) in (emoticons ?? []).enumerated() {
                
                // 1> 取出按钮
                if let btn = contentView.subviews[i] as? UIButton {
                    // 设置图像 - 如果图像为 nil 会清空图像，避免复用
                    btn.setImage(em.image, for: [])
                    
                    // 设置 emoji 的字符串 - 如果 emoji 为 nil 会清空 title，避免复用
                    btn.setTitle(em.emoji, for: [])
                    
                    btn.isHidden = false
                }
            }
          print("表情包数量 \(emoticons?.count)")
           
        }
    }
    
    
    @IBOutlet weak var label: UILabel!

    override init(frame: CGRect) {
       
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 监听方法
    @objc private func selectedEmoticonButton(button: UIButton) {
        
        let tag = button.tag
        
        var em: JKEmoticon?
        // 判断是不是删除按钮, 不是就取得表情
        if tag < emoticons?.count {
            em = emoticons?[tag]
        }
//        print(em)
        delegate?.emoticonCellDidSelectedEmoticon(cell: self, em: em)
    }
    
    /// 长按手势识别
    @objc private func longGesture(gesture: UILongPressGestureRecognizer) {
        
    }
    
    
}


// MARK: - 设置界面

private extension CZEmoticonCell {

    // - 从 XIB 加载, bounds 是 XIB 中定义的大小, 不是 size 的大小
    // - 从纯代码创建, bounds 是布局属性中设置的 itemSize
    func setupUI() {
    
        let rowCount = 3
        let colCount = 7
        
        let leftMargin: CGFloat = 8
        
        let bottomMargin: CGFloat = 16
        
        let w = (bounds.width - 2 * leftMargin) / CGFloat(colCount)
        
        let h = (bounds.height - bottomMargin) / CGFloat(rowCount)
        
        for i in 0..<21 {
            let row = i / colCount
            let col = i % colCount
            
            let btn = UIButton()
            
            let x = leftMargin + CGFloat(col) * w
            let y = CGFloat(row) * h

            btn.frame = CGRect(x: x, y: y, width: w, height: h)
            
//            btn.backgroundColor = UIColor.red()
            
            contentView.addSubview(btn)
            
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 32)

            btn.tag = i
            
            btn.addTarget(self, action: #selector(selectedEmoticonButton), for: .touchUpInside)
        }
        
        // 取出末尾的删除按钮
        let removeButton = contentView.subviews.last as! UIButton
        
        // 设置图像
        let image = UIImage(named: "compose_emotion_delete_highlighted", in: JKEmoticonManager.shared.bundle, compatibleWith: nil)
        removeButton.setImage(image, for: [])
        
        // 添加长按手势
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longGesture))
        
        
        longPress.minimumPressDuration = 0.1
        addGestureRecognizer(longPress)
        
    }
}
